/**
 * Created by asus on 2017/4/24.
 */

/*
var getHeader = function(index){
    var headerHtml =
        '<div class="navWrapper">'+
        '<div id="headerWrapper" class="clearfix">'+
        '<img id="logo" class="fl" src="../img/logo.png" alt="">'+
        '<ul class="fl">'+
        '<li><a class="" href="../index.html">首页</a><i class="nav_trangle displayNone"></i></li>'+
        '<li><a href="advance.html">直播预告</a><i class="nav_trangle displayNone"></i></li>'+
        '<li><a href="starCircle.html">星友圈</a><i class="nav_trangle displayNone"></i></li>'+
        '<li><a href="#">星部落</a><i class="nav_trangle displayNone"></i></li>'+
        '</ul>'+
        '<ul class="fr">'+
        '<li class="searchWrapper">'+
        '<i class="searchIcon"></i>'+
        '<input id="search" type="text" placeholder="明星">'+
        '</li>'+
        '<li><a href="#">app下载</a><i class="nav_trangle displayNone"></i></li>'+
        '<li><a href="#">充值</a><i class="nav_trangle displayNone"></i></li>'+
        '<li style="vertical-align: middle">'+
        '<img class="user_mess" src="../img/message.png" alt="" style="vertical-align: middle">'+
        '</li>'+
        '<li>'+
        '<img class="user_img" src="../img/user_img.png" alt="" style="vertical-align: middle">'+
        '</li>'+
        '</ul>'+
        '</div>'+
        '</div>';
    $(headerHtml).prependTo("body");

    var nav = $("#headerWrapper ul li a");
    nav.eq(index).addClass("active").siblings("i").removeClass("displayNone");
    nav.hover(function(){
        $(this).siblings("i").stop().removeClass("displayNone");
        $(this).addClass("active");
        //$(this).siblings("i").stop().fadeIn("fast").animateCss("fadeInDown");
    },function(){
        $(this).siblings("i").stop().addClass("displayNone");
        $(this).removeClass("active");
        nav.eq(index).addClass("active").siblings("i").removeClass("displayNone");
    });

    var user_addr = '<p class="user_addr">您的位置：<span>'+nav.eq(index).html()+'</span></p>';
    $(user_addr).prependTo(".center");

};
*/

var headerMove = function(index){
    var nav = $("#headerWrapper ul li.nav_click");
    nav.eq(index).children("a").addClass("active").siblings("i").removeClass("displayNone");
    nav.hover(function(){
        $(this).children("a").siblings("i").stop().removeClass("displayNone");
        $(this).children("a").addClass("active");
        //$(this).siblings("i").stop().fadeIn("fast").animateCss("fadeInDown");
    },function(){
        $(this).children("a").siblings("i").stop().addClass("displayNone");
        $(this).children("a").removeClass("active");
        nav.eq(index).children("a").addClass("active").siblings("i").removeClass("displayNone");
    });
    var addr_info = nav.eq(index).html();
    if(index==6){
        addr_info = "消息中心";
        $("#icon_message").addClass("ic_message");
    }else if(index==7){
        addr_info = "个人中心";
    }
    if(index!=6){
        $("#icon_message").parent("li").hover(function(){
            $(this).children("#icon_message").addClass("ic_message");
        },function(){
            $(this).children("#icon_message").removeClass("ic_message");
        });
    }
   /* var user_addr = '<p class="user_site">您的位置：<span>'+addr_info+'</span></p>';
    $(".navWrapper").after(user_addr);*/
    //$(user_addr).prependTo(".navWrapper");
    $("#headerWrapper").find("#personal,#down_load_btn,#message_btn").hover(function(){
        $(this).find("div").stop().slideDown("fast");
    },function(){
        $(this).find("div").stop().slideUp("fast");
    });
};


//取出用户信息
$(function(){

    var log_flag = $.cookie("exist", {
        name: 'login'
    });
    if(!log_flag){
        window.location.href="../../Login/index.html";
    }

    var login_cookie = $.cookie("get", {
        name: 'login'
    });
    login_cookie = $.parseJSON(login_cookie);
    $(".user_name").html(login_cookie.data.u_nickname);
    $(".user_img").attr("src",localStorage.u_avatar);

    //退出登录
    $(".logout").on("click",function(){
        $.cookie("delete", {
            name: 'login'
        });
        var login_flag = $.cookie("exist", {
            name: 'login_flag'
        });
        if(login_flag){
            $.cookie("delete", {
                name: 'login_flag'
            });
        }
        localStorage.clear();
        window.location.href="../../Login/index.html"
    });

    //是否有新的消息


    if(localStorage.newMessage){
        if(localStorage.newMessage!=0){
            $("#icon_message").children(".ic_cir").show();
        }else {
            $("#icon_message").children(".ic_cir").hide();
        }
    }else {
        var param = {
            url:'Messages/unread'
        };
        $.JsonRpc(param,function(data){
            localStorage.newMessage = data.data.num;
            if(localStorage.newMessage!=0){
                $("#icon_message").children(".ic_cir").show();
            }else {
                $("#icon_message").children(".ic_cir").hide();
            }
        });
    }


    /*document.onkeydown = function(e){
        var ev = document.all ? window.event : e;
        if(ev.keyCode==13) {
            //$('#FormId).submit();
        }
    };*/

   /* //关注
    var focus_Fun = function(that){
        //Users/subscribe
        var that = $(that);
        sid = that.data("sid");
        s_status = that.data("s_status");

        var search_param = {
            url:'Users/subscribe',
            data:{
                follow:sid
            }
        };
        $.JsonRpc(search_param,function(data){
            if(data.data.status==10){
                that.html("已关注")
                    .addClass("btn_cancel")
                    .removeClass("btn_confirm");
            }else if(data.data.status==20){
                that.html("关注")
                    .addClass("btn_confirm")
                    .removeClass("btn_cancel");
            }

          /!*  $(".focus_btn.btn_cancel").hover(function(){
                $(this).html("取消关注");
            },function(){
                $(this).html("已关注");
            });*!/

        });

    };*/

    //搜索
    var search_fun = function(){
        var open_search_wra = function(input_val){
            function get_searchs(content){
                var search_param = {
                    url:'Abouts/search',
                    data:{
                        content:content
                    }
                };
                $.JsonRpc(search_param,function(data){
                    if(data.data.list.length==0){
                        var prompt_info = "<li>未搜索到相关明星</li>";
                        $(".search_list").html(prompt_info);
                        return false;
                    }
                    data = data.data.list;
                    var tpl="";
                    $.each(data,function(k){
                        tpl+=
                            '<li class="clearfix">'+
                            '<a href="##"><img class="star_img fl" src="'+data[k].u_avatar+'" alt=""></a>'+
                            '<a href="##" class="info fl">'+
                            '<h5>'+data[k].s_name+'</h5>'+
                            '<p>'+data[k].s_booked+'人关注</p>'+
                            '</a>'+iService.focus_status_btn(data[k].booked,data[k].suid)+
                            '</li>'
                        ;
                    });
                    var search_list = $(".search_list");
                    search_list.html(tpl);
                    search_list.off("click").on("click",".focus_btn",function(){
                        iService.focus_Fun(this);
                    });
                    search_list.off("mouseover mouseout").on("mouseover mouseout",".btn_cancel",function(event){
                        if(event.type == "mouseover"){
                            $(this).html("取消关注");
                        }else if(event.type == "mouseout"){
                            $(this).html("已关注");
                        }
                    });

                });
            }
            var searchHtml =
                '<div id="search_list_wra">'+
                '<div class="search_top clearfix">'+
                '<div id="form_wra" class="fl">'+
                '<form id="search_form" action="#" onsubmit="return false;">'+
                '<input class="secrch_key fl" type="search" name="search" autocomplete="off"id="J_search" placeholder="搜索" value="'+input_val+'">'+
                '<input type="submit" class="submit fr" id="" value="">'+
                '</form>'+
                '</div>'+
                '<span class="cancel_btn fr">取消</span>'+
                '</div>'+
                '<div class="search_title">搜索：'+input_val+'</div>'+
                '<ul class="search_list">'+
                    /*'<li class="clearfix">'+
                    '<a href="##"><img class="star_img fl" src="" alt=""></a>'+
                    '<a href="##" class="info fl">'+
                    '<h5>刘涛</h5>'+
                    '<p>10000人关注</p>'+
                    '</a>'+
                    '<a class="focus_btn fr" href="javascript:void(0)">'+
                    '<i class="ic_follow"></i>关注'+
                    '</a>'+
                    '</li>'+*/
                '</ul>'+
                '</div>';

            layui.use('layer', function(){
                var layer = layui.layer;
                layer.ready(function(){
                    var index = layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        shadeClose: false, //开启遮罩关闭
                        area: ['560px', ''],
                        skin: 'layui-layer-molv',
                        content: searchHtml,
                        resize:true,
                        offset:'150px',
                        success:function(){
                            get_searchs(input_val);
                            var $search_input_ac = $("#J_search");
                            var $search_submit = $("#search_form").children(".submit");
                            $search_input_ac.keydown(function(e) {
                                if (e.keyCode == 13) {
                                    get_searchs($search_input_ac.val());
                                    $(".search_title").html('搜索：'+$search_input_ac.val());
                                }
                            });
                            $search_submit.on("click",function(){
                                get_searchs($($search_input_ac).val());
                                $(".search_title").html('搜索：'+$search_input_ac.val());
                            });
                        }
                    });
                    //  layer.close(index);
                    //  console.log(index);
                    $(".cancel_btn").on("click",function(){
                        layer.close(index);
                    });
                });
            });

        };
        var nav_search_input =$('#search'),
            searchIcon = $(".searchIcon");
        nav_search_input.keydown(function(e) {
            if (e.keyCode == 13) {
                $(this).blur();
                open_search_wra($(nav_search_input).val());
                //$('#FormId).submit(); //处理事件
            }
        });
        searchIcon.on("click",function(){
            open_search_wra($(nav_search_input).val());
        });
    };
    search_fun();
});























